package com.icecloud.cloud.filestore.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.provider.ClientAlreadyExistsException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.icecloud.cloud.filestore.common.RestfulResponseBody;
import com.icecloud.cloud.filestore.service.ClientDetailStoreService;

/**
* @author penghaozhong 
* E-mail:penghaozhong@163.com
* @version 2016年11月14日 下午10:31:01
* 客户端action
*/
@RestController
public class ClientDetailsController {
	
	@Autowired
	private ClientDetailStoreService clientDetailStoreService;
	
	
	/**
	 * 添加客户
	 * @param baseClientDetails
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value =  "/client", method=RequestMethod.POST)
	public ResponseEntity<RestfulResponseBody> client(@RequestBody BaseClientDetails baseClientDetails) throws Exception {
		RestfulResponseBody body = new RestfulResponseBody();
		try {
			clientDetailStoreService.addClientDetails(baseClientDetails);
			body.setReason("保存成功");
			body.setResultFlag(1);
		}catch(ClientAlreadyExistsException ce){
			body.setReason("保存失败,此用户信息已进行存在！");
			body.setResultFlag(-1);
		} catch (Exception e) {
			e.printStackTrace();
			body.setReason("保存失败");
			body.setResultFlag(-1);
		}
		return new ResponseEntity<RestfulResponseBody>(body,HttpStatus.OK);
		
	}
	
	
	@RequestMapping(value =  "/client")
	public ResponseEntity<RestfulResponseBody> list() throws Exception {
		RestfulResponseBody body = new RestfulResponseBody();
			body.put("list",clientDetailStoreService.list());
		return new ResponseEntity<RestfulResponseBody>(body,HttpStatus.OK);
		
	}

}
